package com.asj.service.control;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;


import com.asj.MainActivity;
import com.asj.receiver.MainDeviceAdminReceiver;

/**
 * 关于控制设备方面的服务
 * */
public class ControlDeviceService {
    private final Context context;

    public ControlDeviceService(Context context){
        this.context = context;
    }


    /**
     * 设置应用拥有管理员权限
     * */
    public void startActivityForResult(MainActivity mainActivity){
        //请求用户激活设备管理员
        ComponentName componentName = new ComponentName(mainActivity, MainDeviceAdminReceiver.class);
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "需要激活设备管理员权限");
        mainActivity.startActivityForResult(intent, 1);
    }

    /**
     * 取消应用拥有管理员权限
     * */
    public void removeActiveAdmin(MainActivity mainActivity){
        //取消管理员权限
        DevicePolicyManager dpm = (DevicePolicyManager) mainActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName componentName = new ComponentName(mainActivity, MainDeviceAdminReceiver.class);
        if (dpm.isAdminActive(componentName)) {
            dpm.removeActiveAdmin(componentName);
        }
    }



}
